/***
 * 
 */
package ar.edu.utn.frsf.cidisi.ginet.diagram.part;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.internal.Workbench;
import org.pnml.tools.epnk.pnmlcoremodel.Page;
import org.pnml.tools.epnk.pnmlcoremodel.PetriNet;
import org.pnml.tools.epnk.pnmlcoremodel.PetriNetDoc;
import org.pnml.tools.epnk.pnmlcoremodel.PetriNetType;

import ar.edu.utn.frsf.cidisi.ginet.generation.abstract2concrete.AbstractGIM2ConcreteGIM;
import ar.edu.utn.frsf.cidisi.ginet.verification.formalmodel.FormalModel;

public class VerifyCBPModelAction implements IObjectActionDelegate {
	
	private IFile element;
	
	private Shell shell;


//	public boolean isEnabled(PetriNet petrinet) {
//		if (petrinet != null) {
//			PetriNetType type = petrinet.getType();
//			return type != null && type instanceof GINet;
//		}
//		return false;
//	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	final public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}
	
	
	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	final public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
//			EObject rootElement = getRootElement();
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			
			if (structuredSelection.size() == 1
					&& structuredSelection.getFirstElement() instanceof IFile) {
				element = (IFile)structuredSelection.getFirstElement();

//				((Page)structuredSelection.getFirstElement()).eContainer();
//				abstractGIModule = (Page)structuredSelection.getFirstElement();
//				ginet = (PetriNet)abstractGIModule.eContainer();
			}

		}
//		action.setEnabled(isEnabled(petrinet));
	}
	
	
	/**
	 * @see IActionDelegate#run(IAction)
	 */
	final public void run(IAction action) {
//		Workbench.getInstance().getActiveWorkbenchWindow().getActivePage()
		
////	    Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().
//
//		AbstractGIM2ConcreteGIM agim2cgim = new AbstractGIM2ConcreteGIM(ginet, type);
//		
//		// Creates the Petri Net document
//		PetriNetDoc pnDoc = agim2cgim.createPetriNetDoc(abstractGIModule);
//		
//		// Saves the net to a pnml file
//		agim2cgim.createPNMLFile(pnDoc);
		
		FormalModel formalModel = new FormalModel(element);
		formalModel.generateFormalModel();
//		
		MessageDialog.openInformation(
				shell,
				"Verification",
				"No errors found! :P ");	
//				"Generate GI-Net was executed.");	
//		
////		AbstractEPNKJob job = createJob(petrinet,defaultInput);
////		
////		if (job.prepare()) {
////			defaultInput = job.getInput();
////			// eki: Since there are porblems with user jobs, I do not
////			// start the job as a user job right now.
////			// job.setUser(true);
////			job.setPriority(Job.SHORT);
////			job.schedule(); 
////		}
	}
	
	private EObject getRootElement(){
		
		return null;
	}

	

}
